package mao.redis.jedispool;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * JedisPool
 *
 * @author Totoro
 * @date 2020/5/27
 * @explain:bug排除，使用稳定的JavajDK
 */
public class MyJedisPool {
    /**
     * jedisPoll=应该是单例模式，这个类相当于对jedispool进行包装，保证其单例模式。
     */
    private static String ADDR = "192.168.213.129";
    private static Integer PORT = 6379;
    private static JedisPool jedisPool;

    private MyJedisPool() {}

    public static Jedis getJedis(){
        if (jedisPool == null){
            synchronized (MyJedisPool.class){
                JedisPoolConfig config = new JedisPoolConfig();
                config.setMaxTotal(1024);
                config.setMaxIdle(30);
                config.setMaxWaitMillis(1000);
                config.setTestOnBorrow(true);
                jedisPool = new JedisPool(config,ADDR, PORT,10000);
            }
        }
        return jedisPool.getResource();
    }

    //实际是进行归还连接池操作。
    public static void close(Jedis jedis){
        jedis.close();
    }
}
